package just.stuff.morsels.ui.morsel;

import java.util.List;

import just.stuff.morsels.objects.Morsel;
import just.stuff.morsels.ui.label.LabelList;
import just.stuff.morsels.util.DateIntervalFormat;
import just.stuff.morsels.util.TextLink;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

/**
 * Panel for displaying a list of morsels
 */
public class MorselList extends Panel {

	public MorselList(String id, IModel<? extends List<? extends Morsel>> model) {
		super(id);
		ListView<Morsel> list = new ListView<Morsel>( "list", model ) {
			@Override
			protected void populateItem(ListItem<Morsel> item) {
				TextLink<?> link = new TextLink<Object>("link", MorselPage.class, Model.of( item.getModelObject().getTitle() ) );
				link.setParameter( "id", item.getModelObject().getId() );
				item.add( link );
				item.add( new LabelList( "labels", item.getModel()));
				item.add( new Label( "relevance", String.valueOf( item.getModelObject().getRelevance() ) ) );
				item.add( new Label( "lastSaved", DateIntervalFormat.format( item.getModelObject().getLastSaved() ) ) );
				
				
			}
		};
		add( list );
		
	}

}
